import requests
import urllib3

from get_token.token_manager import TokenManager
from utils.config import host_port


# api
class DeckTypeApi():

    def __init__(self):
        self.token = TokenManager.getToken()

    # 新增
    def addDeckType(self, name):
        url = f'https://{host_port}/decklist/decktype/save'
        # 传入token
        header = {"token": self.token, 'Content-Type': 'application/json'}
        payload = {"name": name}
        urllib3.disable_warnings()
        r = requests.post(url, json=payload, headers=header, verify=False)
        return r.json()['code']

    # 查询
    def queryDeck(self, name):
        url = f'https://{host_port}/decklist/decktype/list'
        header = {'token': self.token}
        payload = {'key': name}
        urllib3.disable_warnings()
        r = requests.get(url, params=payload, headers=header, verify=False)
        code_ = r.json()['code']
        if code_ == 0:
            return r.json()['page']['list'][0]['name']
        elif code_ == 401:
            TokenManager().clearTxtSession()
            TokenManager().getToken()
        return ''
